﻿/* 
 *  <copyright file="PlatformerScreen.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using Microsoft.Xna.Framework;
using SEGameExample.Kludge;
using SobrietyEngine.Actor;
using SobrietyEngine.Components;
using SobrietyEngine.Guids;
using SobrietyEngine.Logging;
using SobrietyEngine.Scene;
using SobrietyEngine.Screens;
using SobrietyEngine.UDMF;
using SEGameExample.InputMappers;

namespace SEGameExample.Screens
{
    class UDMFTest: BaseLevel 
    {
        public UDMFTest()
            : base("UDMFTestScene")
        {
        }

        public override void Initialize()
        {
            base.Initialize();

            //set some options
            //turn on logging of some events
            EventRouter.LogRaisedEvents = true;
            EventRouter.LogEventIgnore.Add("Input.");
            EventRouter.LogEventIgnore.Add(".Move");
            EventRouter.LogEventIgnore.Add(".Rotate");
            EventRouter.LogEventIgnore.Add(".ActorInit");

            //set some logging options
            Logger.AppendLoggingType(Logger.LogType.AssertOnError |
                                     Logger.LogType.VSConsole |
                                     Logger.LogType.RaiseLogEvent);

            //set up our PlayerOne (first found user with an Xbox 360 controller)
            //turn off any controllers that may be active (shoudl always disable controllers when you are finished!)
            PlayerOne.DisableAllInputMappers();
            if(!PlayerOne.HasInputMapper(typeof(PlatformerEventMapper)))
                PlayerOne.AddInputMapper(new PlatformerEventMapper(EventRouter, PlayerOne.Guid));
            PlayerOne.EnableInputMapper(typeof(PlatformerEventMapper));

            //forward declaration of some variables used in here
            IActor actor = null;
            IScene scene = SceneGameService.Scenes[SceneName];

            //test loading UDMF formatted actor tempaltes
            actorGameService.LoadActorTemplates("actors/actorDefs");

            IActor udmfMapActor = SobrietyEngine.Actor.Builders.UDMF.UDMFMapBldr.Build(Scene, "Scenes/testUDMF", 
                new Vector3(0, 0, -2.0f), Vector3.Forward, new Vector3(0.07f, 0.07f, 0.07f));

            /*
            //rotate udmf map for fun
            IComponent c = new SobrietyEngine.Components.Common.ConstantRotationCmp(0.1f, 0, 0);
            udmfMapActor.AttachComponent(c);
            */

            //lets try getting the player start pos from the actors name using guids
            //this is assuming there is an actor out there named 'playerstart'
            CGuid playerStart = CGuid.GetGuid("playerstart");
            IActor startAct = playerStart.Resolve<IActor>();
            IPosition startPosCmp = startAct.Get<IPosition>();
            Vector2 startPos = startPosCmp.PositionV2;

            
            //Test some gui and hud stuff
            //create a 2D gui camera     
            //IActor cam2D = GUISpawner.Camera2D(Scene, ScreenManager.FullScreenViewport);
            IActor cam2d = SobrietyEngine.Actor.Builders.Cameras.GuiCameraBldr.Build(Scene, ScreenManager.FullScreenViewport);

            //some help text about controls
            String help = "UDMF from DoomBuilder. \n ";
            help += "Left thumbstick moves\n";
            help += "(A) jumps, (RB) super jumps, \n";
            help += "(B) exits, (Y) toggles physics debug \n";
            help += "(LB) toggle debug console\n";

            SobrietyEngine.Actor.Builders.GUI.Text2D.Build(Scene, new Vector2(0, ScreenManager.FullScreenViewport.Height / 2), help);

            //create out platform player 
            IActor playerActor = GameplaySpawner.PlatformerPlayer2D(scene, startPos, PlayerOne.Guid, ScreenManager.GraphicsDevice, FarseerPhysics.Dynamics.Category.Cat1, textureManager);
            //playerActor.Get<IFarseerPhysicsBody>().Body.IgnoreGravity = true;

            //Ortho (2d style) cam that follows an actor
            actor = GameplaySpawner.OrthoFollowActorCam3D(scene, ScreenManager.FullScreenViewport, ScreenManager.GraphicsDevice, playerActor.Guid);

            //lets atach a aprticle effect to the player
            actor = scene.CreateEmptyActor();

            SobrietyEngine.Components.Particles.MercuryParticleBase p =
                new SobrietyEngine.Components.Particles.MercuryParticleBase();

            actor.AttachComponent(p);
            Vector3 ppos = new Vector3(0, 0, -1);
            actor.AttachComponent(new SobrietyEngine.Components.Common.PositionCmp(ppos));
            actor.Initialize();


            //turn on our controllers
            SceneGameService.Scenes[SceneName].EnableControllers();
        }

    }
}